/*
 * 文 件    名:  OrderProductModel.java
 * 创建日期:  2012-10-25上午11:49:46
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  df085
 */
package com.weipu.common.facade.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.weipu.common.constants.ServerConstant;

/**
 * @Description 用户新增的地址
 * @version [版本号, 2012-10-25]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author hf
 */
public class AddressModel implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1903827421682186834L;

	/**
	 * 用户CustomerId
	 */
	private long CustomerID;

	/**
	 * 地址类型
	 */
	private int AddressType;

	private Address address;

	/**
	 * AddressID
	 */
	private long AddressID;

	/**
	 * 国家名称
	 */
	private String CountryName;
	
	private String token;

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public static class Address implements Serializable {
		/**
		 * 
		 */
		private static final long serialVersionUID = -2525445592059521628L;

		/**
		 * 第一名称
		 */
		private String FirstName;

		/**
		 * 第二名称
		 */
		private String LastName;

		/**
		 * 国家码
		 */
		private String CountryCode;
		private String Country;

		/**
		 * 州
		 */
		private String State;

		/**
		 * 市
		 */
		private String City;

		/**
		 * 街道1
		 */
		private String Street1;

		/**
		 * 街道2
		 */
		private String Street2="";

		/**
		 * 邮编
		 */
		private String PostalCode;

		/**
		 * phone
		 */
		private String PhoneNumber;
		/**
		 * phone
		 */
		private String Phone;

		/**
		 * 邮箱
		 */
		private String email="";

		/**
		 * 电话号码
		 */
		private String MobilePhone;
		/**
		 * 电话号码2
		 */
		private String Mobile;

		/**
		 * 是否为默认地址
		 */
		private int IsDefault;
		
		public String getCountry() {
			return Country;
		}

		public void setCountry(String country) {
			Country = country;
		}

		public String getPhone() {
			return Phone;
		}

		public void setPhone(String phone) {
			Phone = phone;
		}

		public String getMobile() {
			return Mobile;
		}

		public void setMobile(String mobile) {
			Mobile = mobile;
		}

		public String getFirstName() {
			return FirstName;
		}

		public void setFirstName(String firstName) {
			FirstName = firstName;
		}

		public String getLastName() {
			return LastName;
		}

		public void setLastName(String lastName) {
			LastName = lastName;
		}

		public String getCountryCode() {
			return CountryCode;
		}

		public void setCountryCode(String countryCode) {
			CountryCode = countryCode;
		}

		public String getState() {
			return State;
		}

		public void setState(String state) {
			State = state;
		}

		public String getCity() {
			return City;
		}

		public void setCity(String city) {
			City = city;
		}

		public String getStreet1() {
			return Street1;
		}

		public void setStreet1(String street1) {
			Street1 = street1;
		}

		public String getStreet2() {
			return Street2;
		}

		public void setStreet2(String street2) {
			Street2 = street2;
		}

		public String getPostalCode() {
			return PostalCode;
		}

		public void setPostalCode(String postalCode) {
			PostalCode = postalCode;
		}

		public String getPhoneNumber() {
			return PhoneNumber;
		}

		public void setPhoneNumber(String phoneNumber) {
			PhoneNumber = phoneNumber;
		}

		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public String getMobilePhone() {
			return MobilePhone;
		}

		public void setMobilePhone(String mobilePhone) {
			MobilePhone = mobilePhone;
		}

		public int getIsDefault() {
			return IsDefault;
		}

		public void setIsDefault(int isDefault) {
			IsDefault = isDefault;
		}
		
		@Override
        public String toString()
        {
            return "ShipAddress [FirstName=" + FirstName + ", LastName=" + LastName + ", PhoneNumber=" + PhoneNumber
                + ", Email=" + email + ", Mobile=" + MobilePhone + ", PostalCode=" + PostalCode + ", Street1=" + Street1
                + ", Street2=" + Street2 + ", City=" + City + ", State=" + State + ", Country=" + CountryCode + ", IsDefault=" + IsDefault+"]";
        }

	}

	public long getCustomerID() {
		return CustomerID;
	}

	public void setCustomerID(long customerID) {
		CustomerID = customerID;
	}

	public int getAddressType() {
		return AddressType;
	}

	public void setAddressType(int addressType) {
		AddressType = addressType;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public long getAddressID() {
		return AddressID;
	}

	public void setAddressID(long addressID) {
		AddressID = addressID;
	}

	public String getCountryName() {
		return CountryName;
	}

	public void setCountryName(String countryName) {
		CountryName = countryName;
	}
	
	@Override
    public String toString()
    {
        return "ShipAddress "+address;
    }

	public AddressModel() {

	}

	public static List<AddressModel> jsonArray2Address(JSONArray jsonArray) {
		List<AddressModel> addressModels = new ArrayList<AddressModel>();
		if (null != jsonArray) {
			for (int i = 0; i < jsonArray.length(); i++) {
				AddressModel addressModel = new AddressModel();
				JSONObject jObject = jsonArray.optJSONObject(i);
				addressModel.setAddressID(jObject
						.optLong(ServerConstant.Address.CONSTANT_ADDRESSID));
				if (jObject
						.optString(ServerConstant.Address.CONSTANT_COUNTRYNAME) != JSONObject.NULL
						&& !jObject
								.isNull(ServerConstant.Address.CONSTANT_COUNTRYNAME)) {
					addressModel
							.setCountryName(jObject
									.optString(ServerConstant.Address.CONSTANT_COUNTRYNAME));
				}
				JSONObject addressObject = jObject
						.optJSONObject(ServerConstant.Address.CONSTANT_ADDRESS);
				Address address = new Address();
				if (null != addressObject) {
					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_FIRSTNAME) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_FIRSTNAME)) {
						address.setFirstName(addressObject
								.optString(ServerConstant.Address.CONSTANT_FIRSTNAME));
					}
					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_LASTNAME) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_LASTNAME)) {
						address.setLastName(addressObject
								.optString(ServerConstant.Address.CONSTANT_LASTNAME));
					}
//					if (addressObject
//							.optString(ServerConstant.Address.CONSTANT_COUNTRYCODE) != JSONObject.NULL
//							&& !addressObject
//									.isNull(ServerConstant.Address.CONSTANT_COUNTRYCODE)) {
//						address.setCountryCode(addressObject
//								.optString(ServerConstant.Address.CONSTANT_COUNTRYCODE));
//					}
					//由于Address CountryCode返回的是数字 所以引用外传的CountryName 国家简称
					if (jObject
							.optString(ServerConstant.Address.CONSTANT_COUNTRYNAME) != JSONObject.NULL
							&& !jObject
									.isNull(ServerConstant.Address.CONSTANT_COUNTRYNAME)) {
						address.setCountryCode(jObject
										.optString(ServerConstant.Address.CONSTANT_COUNTRYNAME));
					}
//					address.setCountryCode(addressObject
//							.optInt(ServerConstant.Address.CONSTANT_COUNTRYCODE));

					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_STATE) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_STATE)) {
						address.setState(addressObject
								.optString(ServerConstant.Address.CONSTANT_STATE));
					}
					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_CITY) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_CITY)) {
						address.setCity(addressObject
								.optString(ServerConstant.Address.CONSTANT_CITY));
					}
					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_STREET1) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_STREET1)) {
						address.setStreet1(addressObject
								.optString(ServerConstant.Address.CONSTANT_STREET1));
					}
					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_STREET2) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_STREET2)) {
						address.setStreet2(addressObject
								.optString(ServerConstant.Address.CONSTANT_STREET2));
					}
					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_STREET2) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_STREET2)) {
						address.setStreet2(addressObject
								.optString(ServerConstant.Address.CONSTANT_STREET2));
					}
					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_POSTALCODE) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_POSTALCODE)) {
						address.setPostalCode(addressObject
								.optString(ServerConstant.Address.CONSTANT_POSTALCODE));
					}

					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_PHONENUMBER) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_PHONENUMBER)) {
						address.setPhoneNumber(addressObject
								.optString(ServerConstant.Address.CONSTANT_PHONENUMBER));
					}
					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_EMAIL) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_EMAIL)) {
						address.setEmail(addressObject
								.optString(ServerConstant.Address.CONSTANT_EMAIL));
					}
					if (addressObject
							.optString(ServerConstant.Address.CONSTANT_MOBILEPHONE) != JSONObject.NULL
							&& !addressObject
									.isNull(ServerConstant.Address.CONSTANT_MOBILEPHONE)) {
						address.setMobilePhone(addressObject
								.optString(ServerConstant.Address.CONSTANT_MOBILEPHONE));
					}

					address.setIsDefault(addressObject
							.optInt(ServerConstant.Address.CONSTANT_ISDEFAULT));
					addressModel.setAddress(address);

				}
				addressModels.add(addressModel);
			}
		}
		return addressModels;
	}

	public static AddressModel jsonAddress(JSONObject jObject) {

		AddressModel addressModel = new AddressModel();
		addressModel.setAddressID(jObject
				.optLong(ServerConstant.Address.CONSTANT_ADDRESSID));
		if (jObject.optString(ServerConstant.Address.CONSTANT_COUNTRYNAME) != JSONObject.NULL
				&& !jObject.isNull(ServerConstant.Address.CONSTANT_COUNTRYNAME)) {
			addressModel.setCountryName(jObject
					.optString(ServerConstant.Address.CONSTANT_COUNTRYNAME));
		}
		JSONObject addressObject = jObject
				.optJSONObject(ServerConstant.Address.CONSTANT_ADDRESS);
		Address address = new Address();
		if (null != addressObject) {
			if (addressObject
					.optString(ServerConstant.Address.CONSTANT_FIRSTNAME) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_FIRSTNAME)) {
				address.setFirstName(addressObject
						.optString(ServerConstant.Address.CONSTANT_FIRSTNAME));
			}
			if (addressObject
					.optString(ServerConstant.Address.CONSTANT_LASTNAME) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_LASTNAME)) {
				address.setLastName(addressObject
						.optString(ServerConstant.Address.CONSTANT_LASTNAME));
			}
			if (addressObject
					.optString(ServerConstant.Address.CONSTANT_COUNTRYCODE) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_COUNTRYCODE)) {
				address.setCountryCode(addressObject
						.optString(ServerConstant.Address.CONSTANT_COUNTRYCODE));
			}
//			address.setCountryCode(addressObject
//					.optInt(ServerConstant.Address.CONSTANT_COUNTRYCODE));

			if (addressObject.optString(ServerConstant.Address.CONSTANT_STATE) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_STATE)) {
				address.setState(addressObject
						.optString(ServerConstant.Address.CONSTANT_STATE));
			}
			if (addressObject.optString(ServerConstant.Address.CONSTANT_CITY) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_CITY)) {
				address.setCity(addressObject
						.optString(ServerConstant.Address.CONSTANT_CITY));
			}
			if (addressObject
					.optString(ServerConstant.Address.CONSTANT_STREET1) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_STREET1)) {
				address.setStreet1(addressObject
						.optString(ServerConstant.Address.CONSTANT_STREET1));
			}
			if (addressObject
					.optString(ServerConstant.Address.CONSTANT_STREET2) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_STREET2)) {
				address.setStreet2(addressObject
						.optString(ServerConstant.Address.CONSTANT_STREET2));
			}
			if (addressObject
					.optString(ServerConstant.Address.CONSTANT_STREET2) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_STREET2)) {
				address.setStreet2(addressObject
						.optString(ServerConstant.Address.CONSTANT_STREET2));
			}
			if (addressObject
					.optString(ServerConstant.Address.CONSTANT_POSTALCODE) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_POSTALCODE)) {
				address.setPostalCode(addressObject
						.optString(ServerConstant.Address.CONSTANT_POSTALCODE));
			}

			if (addressObject
					.optString(ServerConstant.Address.CONSTANT_PHONENUMBER) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_PHONENUMBER)) {
				address.setPhoneNumber(addressObject
						.optString(ServerConstant.Address.CONSTANT_PHONENUMBER));
			}
			if (addressObject.optString(ServerConstant.Address.CONSTANT_EMAIL) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_EMAIL)) {
				address.setEmail(addressObject
						.optString(ServerConstant.Address.CONSTANT_EMAIL));
			}
			if (addressObject
					.optString(ServerConstant.Address.CONSTANT_MOBILEPHONE) != JSONObject.NULL
					&& !addressObject
							.isNull(ServerConstant.Address.CONSTANT_MOBILEPHONE)) {
				address.setMobilePhone(addressObject
						.optString(ServerConstant.Address.CONSTANT_MOBILEPHONE));
			}

			address.setIsDefault(addressObject
					.optInt(ServerConstant.Address.CONSTANT_ISDEFAULT));
			addressModel.setAddress(address);

		}
		return addressModel;
	}
}
